package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * 
 * @author axi
 *
 */
public class NxNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
			"http://www.nxnews.net/.*system/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9]+.shtml"
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		
		String title="";
		String content="";
		Long date=null;
		Integer commentNum=0;
		Integer clickNum=0;
		NewsMeta news=new NewsMeta();
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		
		String html=doc.select("meta[http-equiv=refresh]").toString();
		String tempUrl=StringUtils.substringBetween(html, "url=", "\"");
		if(StringUtils.isNotBlank(tempUrl)){
			if(!isDetailPage(tempUrl)){
				news.setUpdateUrl(ParseState.ERR404.toString());
				return news;
			}else{
				urlMeta=CrawlHTML.responseToURL(tempUrl);
				if (urlMeta.getHtml() == null) {
				}	
				htmltxt=urlMeta.getHtml();
				doc=Jsoup.parse(htmltxt);
			}
		}
		
		/**
		 * 解析标题
		 */
		Elements selectTitle=doc.select("span.listblackfont20h30heiti");
		if(selectTitle.size() > 0){
			title=selectTitle.text();
		}
		
		if(StringUtils.isBlank(author)){
			author=doc.select("td.Navsl a").text();
		}
		/**
		 * 解析时间
		 */
		Elements selectDate=doc.select("td.Navsl");
		if(selectDate.size() > 0){
			date=DateUtils.matchDate(selectDate.toString());
		}
		/**
		 * 解析内容
		 */
		Elements selectContent=doc.select("td#thetd");
		if(selectContent.size() > 0){
			content=HtmlCleaner.getContentHtml(url,selectContent);
		}
		if("".equals(title)){
		}
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);
		news.setType(0);
		news.setCommentNum(commentNum);
		news.setClickNum(clickNum);
		news.setAuthor(author);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NxNewsAnalyse s=new NxNewsAnalyse();
		String url="http://www.nxnews.net/sz/system/2014/03/30/011005788.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=s.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
